package com.baihan.angus.app.game.busshow.pop_dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.baihan.angus.app.game.basic.utils.AppUtil;
import com.baihan.angus.app.game.busshow.R;


/**
 * Module:   InputTextMsgDialog
 * <p>
 * Function: 观众、主播的弹幕或普通文本的输入框
 */
public class InputTextMsgDialog extends Dialog {
    private static final String TAG = InputTextMsgDialog.class.getSimpleName();

    private Button btnSend;
    private EditText etMsg;
    private Context mContext;
    private WindowManager.LayoutParams mLayoutParams;
    private InputMethodManager mInputMethodManager;
    private OnTextSendListener mOnTextSendListener;

    public InputTextMsgDialog(@NonNull Context context) {
        super(context, R.style.FullDialog);
        this.mContext = context;
        setContentView(R.layout.trtcvoiceroom_dialog_input_text);
        initContentView();
        init(context);
    }

    private void init(Context context) {
        this.mContext = context;
        Window dialogWindow = this.getWindow();
        mLayoutParams = dialogWindow.getAttributes();
        mLayoutParams.width = AppUtil.getScreenWidth(context);
        dialogWindow.setAttributes(mLayoutParams);
        dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
        dialogWindow.getDecorView().setBackgroundResource(android.R.color.transparent);
        dialogWindow.setGravity(Gravity.BOTTOM);
    }

    private void initContentView() {
        View dialogView = LayoutInflater.from(this.getContext()).inflate(R.layout.trtcvoiceroom_dialog_input_text, (ViewGroup) null);
        setContentView(dialogView);
        btnSend = dialogView.findViewById(R.id.btnSend);
        etMsg = dialogView.findViewById(R.id.etMsg);

        etMsg.setInputType(InputType.TYPE_CLASS_TEXT);
        etMsg.requestFocus();
        mInputMethodManager = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        etMsg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String msg = etMsg.getText().toString().trim();
                if (!TextUtils.isEmpty(msg)) {

                    mOnTextSendListener.onTextSend(msg);
                    mInputMethodManager.showSoftInput(etMsg, InputMethodManager.SHOW_FORCED);
                    mInputMethodManager.hideSoftInputFromWindow(etMsg.getWindowToken(), 0);
                    etMsg.setText("");
                    dismiss();
                } else {
                    Toast.makeText(mContext, R.string.trtcvoiceroom_warning_not_empty, Toast.LENGTH_LONG).show();
                }
                etMsg.setText(null);
            }
        });

        etMsg.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                switch (actionId) {
                    case KeyEvent.KEYCODE_ENDCALL:
                    case KeyEvent.KEYCODE_ENTER:
                        if (etMsg.getText().length() > 0) {
                            //                            mOnTextSendListener.onTextSend("" + messageTextView.getText(), mDanmuOpen);
                            //sendText("" + messageTextView.getText());
                            //imm.showSoftInput(messageTextView, InputMethodManager.SHOW_FORCED);
                            mInputMethodManager.hideSoftInputFromWindow(etMsg.getWindowToken(), 0);
                            //                            messageTextView.setText("");
                            dismiss();
                        } else {
                            Toast.makeText(mContext, R.string.trtcvoiceroom_warning_not_empty, Toast.LENGTH_LONG).show();
                        }
                        return true;
                    case KeyEvent.KEYCODE_BACK:
                        dismiss();
                        return false;
                    default:
                        return false;
                }
            }
        });

        btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String msg = etMsg.getText().toString().trim();
                if (!TextUtils.isEmpty(msg)) {

                    mOnTextSendListener.onTextSend(msg);
                    mInputMethodManager.showSoftInput(etMsg, InputMethodManager.SHOW_FORCED);
                    mInputMethodManager.hideSoftInputFromWindow(etMsg.getWindowToken(), 0);
                    etMsg.setText("");
                    dismiss();
                } else {
                    Toast.makeText(mContext, R.string.trtcvoiceroom_warning_not_empty, Toast.LENGTH_LONG).show();
                }
                etMsg.setText(null);
            }
        });

        etMsg.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int i, KeyEvent keyEvent) {
                Log.d("My test", "onKey " + keyEvent.getCharacters());
                return false;
            }
        });
    }

    public void setmOnTextSendListener(OnTextSendListener onTextSendListener) {
        this.mOnTextSendListener = onTextSendListener;
    }

    public interface OnTextSendListener {
        void onTextSend(String msg);
    }
}
